package com.jstyle.jclifexd.activity;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.utils.SharedPreferenceUtils;
import com.jstyle.jclifexd.view.BreathCustomizeDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BreathSettingActivity extends BaseActivity {

    @BindView(R.id.radioGroup_level)
    RadioGroup radioGroupLevel;
    @BindView(R.id.radioGroup_duration)
    RadioGroup radioGroupDuration;
    private BreathCustomizeDialog breathCustomizeDialog;
    public static final String KEY_DURATION="KEY_DURATION";
    public static final String KEY_level="KEY_level";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_breath_setting);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        int duration=SharedPreferenceUtils.getInteger(KEY_DURATION,1);
        int level=SharedPreferenceUtils.getInteger(KEY_level,1);
        if(duration!=0&&duration<5){
            int id = getResources().getIdentifier(("radioButton_duration" + duration),
                    "id", getPackageName());
            radioGroupDuration.check(id);
        }else {
            if(duration>=5)radioGroupDuration.check(R.id.radioButton_duration5);
        }
        if(level==1){
            radioGroupLevel.check(R.id.radioButton_Beginner);
        }else if(level==2){
            radioGroupLevel.check(R.id.radioButton_Junior);
        }else if(level==3){
            radioGroupLevel.check(R.id.radioButton_Skilled);
        }
        radioGroupLevel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int level=1;
                switch (checkedId){
                    case R.id.radioButton_Beginner:
                        level=1;
                        break;
                    case R.id.radioButton_Junior:
                        level=2;
                        break;
                    case R.id.radioButton_Skilled:
                        level=3;
                        break;
                }
                SharedPreferenceUtils.setSpInteger(KEY_level,level);
            }
        });
        radioGroupDuration.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int duration=0;
                switch (checkedId){
                    case R.id.radioButton_duration1:
                        duration=1;
                        break;
                    case R.id.radioButton_duration2:
                        duration=2;
                        break;
                    case R.id.radioButton_duration3:
                        duration=3;
                        break;
                    case R.id.radioButton_duration4:
                        duration=4;
                        break;
                    case R.id.radioButton_duration5:
                        showCustomInputDialog();
                        break;
                }
                if(duration!=0) SharedPreferenceUtils.setSpInteger(KEY_DURATION,duration);
            }
        });
    }

    private void showCustomInputDialog() {
        if(breathCustomizeDialog==null) breathCustomizeDialog=new BreathCustomizeDialog(this,R.style.jstyleDialog_style);
        breathCustomizeDialog.show();
    }

    @OnClick(R.id.iv_breath_setting_off)
    public void onViewClicked() {
        finish();
    }
}
